package com.example.function;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.database.DatabaseHelper;
import com.example.pojo.Course;
import com.example.student.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddCourse extends AppCompatActivity {
    private EditText etCourseId, etCourseName, etCredits;
    private Spinner spinnerCollege, spinnerMajor, spinnerSemester, spinnerCourseType;
    private Button btnSubmit;
    private DatabaseHelper databaseHelper;
    private Map<String, List<String>> collegeMajorMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        databaseHelper = new DatabaseHelper(this);

        // 初始化视图
        initViews();
        // 初始化 Spinner 数据
        initSpinnerData();

        // 提交按钮点击事件
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCourseToDatabase();
            }
        });
    }

    private void initViews() {
        etCourseId = findViewById(R.id.etCourseId);
        etCourseName = findViewById(R.id.etCourseName);
        etCredits = findViewById(R.id.etCredits);
        spinnerCollege = findViewById(R.id.spinnerCollege);
        spinnerMajor = findViewById(R.id.spinnerMajor);
        spinnerSemester = findViewById(R.id.spinnerSemester);
        spinnerCourseType = findViewById(R.id.spinnerCourseType);
        btnSubmit = findViewById(R.id.btnSubmit);
    }

    private void initSpinnerData() {
        // 初始化学院和专业数据
        collegeMajorMap = new HashMap<>();
        collegeMajorMap.put("校公选", List.of("无"));
        collegeMajorMap.put("计算机科学与技术", List.of("物联网工程", "计算机科学与技术", "网络安全"));
        collegeMajorMap.put("商学院", List.of("工商管理", "市场营销", "会计学"));
        collegeMajorMap.put("文学院", List.of("汉语言文学", "新闻学"));
        collegeMajorMap.put("理学院", List.of("数学与应用数学", "物理学", "化学"));
        collegeMajorMap.put("工程学院", List.of("机械工程", "电气工程", "土木工程"));
        collegeMajorMap.put("医学院", List.of("临床医学", "口腔医学", "药学"));
        collegeMajorMap.put("艺术学院", List.of("音乐学", "美术学", "设计学"));
        collegeMajorMap.put("法学院", List.of("法学", "知识产权", "国际法"));
        collegeMajorMap.put("教育学院", List.of("教育学", "心理学", "学前教育"));
        collegeMajorMap.put("农学院", List.of("农学", "园艺学", "植物保护"));

        // 初始化学院下拉框
        List<String> colleges = new ArrayList<>(collegeMajorMap.keySet());
        ArrayAdapter<String> collegeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, colleges);
        collegeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCollege.setAdapter(collegeAdapter);

        // 初始化专业下拉框
        List<String> majors = new ArrayList<>();
        ArrayAdapter<String> majorAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, majors);
        majorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMajor.setAdapter(majorAdapter);

        // 初始化学期下拉框
        ArrayAdapter<CharSequence> semesterAdapter = ArrayAdapter.createFromResource(
                this, R.array.semester_array, android.R.layout.simple_spinner_item);
        semesterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSemester.setAdapter(semesterAdapter);

        // 初始化课程类型下拉框
        ArrayAdapter<CharSequence> courseTypeAdapter = ArrayAdapter.createFromResource(
                this, R.array.course_type_array, android.R.layout.simple_spinner_item);
        courseTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCourseType.setAdapter(courseTypeAdapter);

        // 设置学院 Spinner 的选择监听
        spinnerCollege.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCollege = parent.getItemAtPosition(position).toString();
                updateMajorSpinner(selectedCollege);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    /**
     * 更新专业 Spinner 的内容
     */
    private void updateMajorSpinner(String selectedCollege) {
        List<String> majors = new ArrayList<>();
        majors.addAll(collegeMajorMap.get(selectedCollege)); // 获取对应学院的专业


        ArrayAdapter<String> majorAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, majors);
        majorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMajor.setAdapter(majorAdapter);
    }

    private void addCourseToDatabase() {
        String courseId = etCourseId.getText().toString().trim();
        String courseName = etCourseName.getText().toString().trim();
        String creditsStr = etCredits.getText().toString().trim();
        String college = spinnerCollege.getSelectedItem().toString();
        String major = spinnerMajor.getSelectedItem().toString();
        String semester = spinnerSemester.getSelectedItem().toString();
        String courseType = spinnerCourseType.getSelectedItem().toString();

        if (courseId.isEmpty() || courseName.isEmpty() || creditsStr.isEmpty() ) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }



        // 校验课程号是否为数字
        if (!courseId.matches("\\d+")) {
            Toast.makeText(this, "课程号必须为数字", Toast.LENGTH_SHORT).show();
            return;
        }
        // 学分为数字
        if (!creditsStr.matches("\\d+")) {
            Toast.makeText(this, "学分错误", Toast.LENGTH_SHORT).show();
            return;
        }
        Integer credits = Integer.parseInt(creditsStr);
        if(credits <=0 || credits > 5) {
            Toast.makeText(this, "学分错误", Toast.LENGTH_SHORT).show();
            return;
        }

        Course course = new Course(courseId, courseName, college, major, courseType, credits, semester);
        boolean isSuccess = databaseHelper.insertCourse(course);
        if (isSuccess) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "添加失败，课程号已存在！", Toast.LENGTH_SHORT).show();
        }
    }
}